package com.yellow.cloud.system.service;


import com.google.code.kaptcha.Producer;
import com.yellowframework.cloud.common.constant.Constants;
import com.yellowframework.cloud.common.exception.ExceptionCast;
import com.yellowframework.cloud.common.model.response.CommonCode;
import com.yellowframework.cloud.utils.RedisUtils;
import com.yellowframework.cloud.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;

/**
 * 验证码
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class CaptchaService {

    @Resource
    private Producer producer;

    @Resource
    private RedisUtils redisUtils;

    public BufferedImage getCaptcha(String uuid) {
        if(StringUtils.isEmpty(uuid)){
            ExceptionCast.cast(CommonCode.VERIFICATION_ID_NOT_NULL);
        }
        //生成文字验证码
        String code = producer.createText();

        // 5分钟后过期
        redisUtils.set(Constants.CAPTCHA_CODE_KEY + uuid, code, 300L);

        return producer.createImage(code);
    }
}
